package com.gaoyi.crm.util;

import android.content.Context;
import android.content.Intent;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.appcompat.widget.ListPopupWindow;
import androidx.navigation.NavController;

import com.gaoyi.crm.R;
import com.gaoyi.crm.constant.Constants;
import com.gaoyi.crm.constant.EventCode;
import com.gaoyi.crm.constant.SPName;
import com.gaoyi.crm.control.dialog.BaseFragmentDialog;
import com.gaoyi.crm.control.popup.MyListPopupWindow;
import com.gaoyi.crm.db.bean.Event;
import com.gs.zxinglibrary.zxing.activity.CaptureActivity;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Field;
import java.util.UUID;

public class Utils {

    public static String getGuid() {
        return UUID.randomUUID().toString();
    }

    /**
     * 通过反射设置Spinner下拉列表的高度
     */
    public static void setSpinnerDropHeight(AppCompatSpinner spinner, int height) {
        try {
            Field popup = AppCompatSpinner.class.getDeclaredField("mPopup");
            popup.setAccessible(true);
            ListPopupWindow popupWindow = (ListPopupWindow) popup.get(spinner);
            if (popupWindow != null) {
                popupWindow.setHeight(height);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转扫描二维码界面
     *
     * @param type 1不带按钮  0带按钮
     */
    public static void scan(Context context, ActivityResultLauncher<Intent> resultLauncher, int type) {
        Intent intent = new Intent(context, CaptureActivity.class);
        intent.putExtra(CaptureActivity.TYPE, type);
        resultLauncher.launch(intent);
    }

    public static void onPauseDialog(BaseFragmentDialog<?> dialog) {
        if (dialog != null && !dialog.isShowing()) {
            dialog.dismissAllowingStateLoss();
        }
    }

    public static void dismissPopupWindow(MyListPopupWindow popupWindow) {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    /**
     * 更新SmartRefreshLayout狀態
     */
    public static void refreshSmartRefreshLayout(SmartRefreshLayout smartRefreshLayout) {
        if (smartRefreshLayout.getTag() == null) {
            return;
        }
        if (smartRefreshLayout.getTag().equals(Constants.REFRESH)) {
            smartRefreshLayout.finishRefresh();
        } else {
            smartRefreshLayout.finishLoadMore();
        }
    }

    public static int getOffset(int pageInx) {
        return (pageInx - 1) * 10;
    }

    public static int getLimit() {
        return 10;
    }

    public static void backToHomePage(NavController controller) {
        if (!controller.popBackStack(R.id.homePageFragment, false)) { // 避免重复生成Fragment
            controller.navigate(R.id.homePageFragment);
        }
    }

    /**
     * 根据中英文获取Customer字段名
     */
    public static String getCustomerName() {
        return LanguageUtils.getInstance().getCustNameFromLan();
    }

    /**
     * 根据中英文获取字段名
     */
    public static String getName() {
        return LanguageUtils.getInstance().getNameFromLan();
    }

    /**
     * 判断是离线/在线
     */
    public static boolean isOnline() {
        return SPUtil.getInstance().getInt(SPName.NET_STATE, Constants.ONLINE) == Constants.ONLINE;
    }

    /**
     * 获取当前User Id
     */
    public static String getUserId() {
        return SPUtil.getInstance().getString(SPName.ID);
    }

    /**
     * 修改当前模式
     * @param state 在线/离线
     */
    public static void changeNetStatus(int state) {
        if (SPUtil.getInstance().getInt(SPName.NET_STATE, Constants.ONLINE) == state) return;
        SPUtil.getInstance().putInt(SPName.NET_STATE, state);
        EventBus.getDefault().post(new Event(EventCode.EVENT_NET_STATE));
    }
}
